<template>
    <div class="components_edit_modal">
        <el-dialog :title="edited?'联系人修改':'联系人添加'" :visible.sync="state" :show-close="true" :close-on-click-modal="false">
            <el-form :model="professionParams"   :rules="rules" ref="professionParams"label-position="right">

                <el-form-item label="学校"  prop="schoolId">
                    <el-select v-model="professionParams.schoolId" placeholder="请选择"  value-key="value"
                            @change="changeSchool">
                    <el-option
                        v-for="item in schoolList" :key="item.id"
                        :label="item.text"
                        :value="item.id">
                    </el-option>
                    </el-select>
                 </el-form-item>

                <el-form-item label="学院" prop="secondSchoolId">
                    <el-select v-model="professionParams.secondSchoolId" placeholder="请选择" value-key="value">
                        <el-option
                             v-for="item in secondSchoolList" :key="item.id"
                            :label="item.text"
                            :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="姓名"  prop="name">
                    <el-input v-model.trim="professionParams.name" placeholder="请输入姓名"></el-input>
                </el-form-item>
                <el-form-item label="职务"  prop="duty">
                    <el-input v-model="professionParams.duty" placeholder="请输入职务"></el-input>
                </el-form-item>
                <el-form-item label="固话"  prop="mobilePhone" >
                    <el-input v-model="professionParams.mobilePhone" placeholder="请输入固话"></el-input>
                </el-form-item>
                <el-form-item label="手机"  prop="telephone" >
                    <el-input type="number" v-model="professionParams.telephone" placeholder="请输入手机"></el-input>
                </el-form-item>
                <el-form-item label="邮箱"  prop="email" >
                    <el-input v-model="professionParams.email" placeholder="请输入邮箱"></el-input>
                </el-form-item>
                <el-form-item label="备注"  prop="remark" >
                    <el-input v-model="professionParams.remark" placeholder="请输入备注"></el-input>
                </el-form-item>


            </el-form>

            <div slot="footer" class="dialog-footer">
                <el-button type="primary" size="small" @click="addProfession('professionParams')">确 定</el-button>
                <el-button @click="state = false"  size="small">关 闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {enums} from '@/constant'
    import PlUpload from "../../../common/PlUpload"
    export default {

        data() {
            //手机号
              var checkMobile = (rule, value, callback) => {
              if (!value) {
                return callback(new Error("手机号不能为空"));
              } else if (!/^[0-9_]{1,}$/.test(value)) {
                return callback(new Error("请输入数字"));
              } else if (value.length != 11) {
                return callback(new Error("请输入11位手机号"));
              }else{
                return callback();
              }
            };
            return {
                 state: false,
                 edited:false,
                 rules: {
                    schoolId: [
                        { required: true, message: '请选择院校', trigger: 'change', type: 'number' }
                    ],
                    secondSchoolId: [
                         { required: true, message: '请选择院系', trigger: 'change', type: 'number' }
                    ],
                    name: [
                        { required: true, message: '姓名不能为空', trigger: 'blur'},
                    ],
                    email: [
                        { required: false, message: '请填写邮箱地址', trigger: 'blur' },
                        { type: "email", message: "请输入正确的邮箱地址", trigger: "blur,change" }
                    ],
                    // telephone: [
                    //     { required: false, message: '请填写手机号', trigger: 'blur' },
                    //     { validator: checkMobile, trigger: "blur" }
                    //   ],
                 },
                professionParams: {
                    schoolId:null,
                    schoolName:null,
                    secondSchoolId:null,
                    secondSchoolName:null,
                    name:null,
                    duty:null,
                    mobilePhone:null,
                    telephone:null,
                    email:null,
                    remark:null
                },
                professionId:null,
                // 学校列表
                schoolList:[],
                // 二级院校列表
                secondSchoolList:[],
            }
        },
        created() {
            // 获取学校数据
            this.getSchoolList();
        },
        mounted() {},
        methods: {
            // 打开模态框
            showDialog(schoolId,secondSchoolId){
                // console.log(schoolId+"-------->"+secondSchoolId);
                let self = this;
                if(this.$refs['professionParams']!==undefined){
                    this.$refs['professionParams'].resetFields();
                }
                if(schoolId != null){
                    this.getSecondSchoolList(schoolId,(response)=>{
                        self.secondSchoolList = response;
                        self.state = true;
                        self.edited=false;
                        self.professionParams.schoolId = schoolId;
                        self.professionParams.secondSchoolId = secondSchoolId;
                    });
                }else{
                    self.state = true;
                    self.edited=false;
                }
            },
            editProfession(id,schoolId){
                let self = this;
                this.state = true;
                this.edited=true;
                if(this.$refs['professionParams']!==undefined){
                    this.$refs['professionParams'].resetFields();
                }
                this.professionId = id;
                this.getSecondSchoolList(schoolId,(response)=>{
                        self.secondSchoolList = response;
                        self.getProfessionDetail(id);
                });
            },
            
            //获取专业详情
            getProfessionDetail(id){
                let vm = this;
                vm.axios.get(api.addressBook.detail+id).then((response) => {
                    if (response.data.success) {
                        let school = response.data.data;
                        let professionParams = {
                            schoolId:school.schoolId,
                            schoolName:school.schoolName,
                            secondSchoolId:school.secondSchoolId,
                            secondSchoolName:school.secondSchoolName,
                            name:school.name,
                            duty:school.duty,
                            mobilePhone:school.mobilePhone,
                            telephone:school.telephone,
                            email:school.email,
                            remark:school.remark
                        };
                        this.professionParams = professionParams;
                    } else {
                        vm.$message.error(response.data.msg);
                    }
                    })
            },
            // 新建专业
            addProfession(professionParams) {
                let vm = this;
                this.$refs['professionParams'].validate((valid) => {
                    if (valid) {
                        let url = api.addressBook.insert;
                        let professionParamsN = this.getPassParams();
                        if(vm.edited){
                            url =api.addressBook.update;
                            professionParamsN.id = vm.professionId;
                            this.axios.put(url,professionParamsN)
                            .then((response) => {
                            if (response.data.success) {
                                vm.$message({
                                    type: 'success',
                                    message: response.data.msg,
                                });
                                vm.state=false;
                                vm.$parent.$refs.addressBookTable.$emit('vuetable:refresh');
                            } else {
                                vm.$message.error(response.data.msg);
                            }
                        })
                        }else{
                            // console.log(JSON.stringify(professionParamsN))
                        this.axios.post(url,professionParamsN)
                        .then((response) => {
                            if (response.data.success) {
                                vm.$message({
                                    type: 'success',
                                    message: response.data.msg,
                                });
                                vm.state=false;
                                vm.$parent.$refs.addressBookTable.$emit('vuetable:refresh');
                            } else {
                                vm.$message.error(response.data.msg);
                            }
                        })
                        }
                    } else {
                        return false;
                    }

                });

            },
            // 获取学校数据
            getSchoolList() {
                let vm = this;
                vm.axios.get(api.getSchoolList).then((response) => {
                    if (response.data.success) {
                        vm.schoolList = JSON.parse(JSON.stringify(response.data.data));
                    } else {
                        vm.$message.error(response.data.msg);
                    }
                })
            },
             
            //学校监听
            changeSchool(schoolId){
                this.professionParams.schoolLeaderName=null;
                this.professionParams.schoolLeader=null;
                
                this.professionParams.secondSchoolId= null;
                this.professionParams.secondSchoolName = null;
                
                this.secondSchoolList = [];
                this.schoolLeaderList = [];
                this.getSecondSchoolList(schoolId,(response)=>{
                    this.secondSchoolList = response;
                });
            },
            // 获取二级院系数据
            getSecondSchoolList(schoolId,success) {
                let vm = this;
                vm.axios.get(api.getSecondSchoolList,{
                    params: {
                        schoolId: schoolId
                    }
                }).then((response) => {
                    if (response.data.success) {
                        success(JSON.parse(JSON.stringify(response.data.data)));
                    } else {
                        vm.$message.error(response.data.msg);
                    }
                })
            },
            //补全 name
            getPassParams(){
                let professionParams = this.professionParams;

                let schoolList = this.schoolList;
                let secondSchoolList = this.secondSchoolList;

                for(let i=0;i<schoolList.length;i++){
                    if(schoolList[i].id == professionParams.schoolId){
                        professionParams.schoolName = schoolList[i].text;
                    }
                }
                for(let i=0;i<secondSchoolList.length;i++){
                    if(secondSchoolList[i].id == professionParams.secondSchoolId){
                        professionParams.secondSchoolName = secondSchoolList[i].text;
                    }
                }
                
                return professionParams;
            },
            clear(){
                this.professionParams={
                    schoolId:null,
                    schoolName:null,
                    secondSchoolId:null,
                    secondSchoolName:null,
                    name:null,
                    duty:null,
                    mobilePhone:null,
                    telephone:null,
                    email:null,
                    remark:null
                }
            },
        },

    };
</script>
<style scoped>

</style>